package com.denso.store.guard;

import com.denso.store.admin.scheduler.SchedulerService;
import com.denso.store.common.Status;
import com.denso.store.common.model.Scheduler;
import com.jfinal.core.Controller;
import com.jfinal.kit.PropKit;
import com.xiaoleilu.hutool.http.HttpUtil;

import java.util.Date;
import java.util.List;

/**
 * Created by lijian on 2017/10/29.
 */
public class GuardController extends Controller {

    private static SchedulerService service = new SchedulerService();

    public void index() {
        List<Scheduler> schedulerList = service.findActive();
        setAttr("schedulerList", schedulerList);
        render("index.html");
    }

    public void arrive() {
        int id = getParaToInt("id");
        Scheduler scheduler = service.findById(id);
        scheduler.setArriveTime(new Date());
        scheduler.setStatus(Status.ARRIVE);
        scheduler.update();

        callStore();

        index();
    }

    public void enter() {
        int id = getParaToInt("id");
        Scheduler scheduler = service.findById(id);
        scheduler.setEnterTime(new Date());
        scheduler.setStatus(Status.IN);
        scheduler.update();

        callStore();

        index();
    }

    public void exit() {
        int id = getParaToInt("id");
        Scheduler scheduler = service.findById(id);
        Date enterTime = scheduler.getEnterTime();
        Date leaveTime = new Date();
        int costMinutes = (int)(leaveTime.getTime() - enterTime.getTime())/1000*60;
        scheduler.setLeaveTime(leaveTime);
        scheduler.setCostTime(costMinutes);
        scheduler.setStatus(Status.OUT);
        scheduler.update();

        index();
    }

    private void callStore() {
        new Thread() {
            public void run() {
                String storeUrl = PropKit.get("storeUrl");
                String result = HttpUtil.get(storeUrl);
            }
        }.start();
    }

}
